package util;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author dqy
 */
public class ResponseUtil {

    /**
     * 返回JSON数据 {"status": 500, "msg": "Server internal error", "data": {}}
     *
     * @param response HttpServletResponse object
     * @throws IOException close HttpServletResponse error
     */
    public static void writeInternalError(HttpServletResponse response) throws IOException {
        write(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Server internal error");
    }

    /**
     * 返回JSON数据 {"status": 400, "msg": "Bad Request", "data": {}}
     *
     * @param response HttpServletResponse object
     * @throws IOException close HttpServletResponse error
     */
    public static void writeBadRequest(HttpServletResponse response) throws IOException {
        write(response, HttpServletResponse.SC_BAD_REQUEST, "Bad request");
    }

    /**
     * 返回JSON数据 {"status": 404, "msg": "Request resource not found", "data": {}}
     *
     * @param response HttpServletResponse object
     * @throws IOException close HttpServletResponse error
     */
    public static void writeNotFound(HttpServletResponse response) throws IOException {
        write(response, HttpServletResponse.SC_NOT_FOUND, "Request resource not found");
    }

    /**
     * 返回JSON数据 {"status": 304 , "msg": "Resource not modified", "data": {}}
     *
     * @param response HttpServletResponse object
     * @throws IOException close HttpServletResponse error
     */
    public static void writeNotModified(HttpServletResponse response) throws IOException {
        write(response, HttpServletResponse.SC_NOT_MODIFIED, "Resource not modified");
    }

    /**
     * 返回JSON数据 {"status": 200 , "msg": "success", "data": {}}
     *
     * @param response HttpServletResponse object
     * @throws IOException close HttpServletResponse error
     */
    public static void writeSuccess(HttpServletResponse response) throws IOException {
        write(response, HttpServletResponse.SC_OK, "success");
    }

    /**
     * 返回JSON数据 {"status": {@link HttpServletResponse}, "msg": {@code message}, "data": {}}
     *
     * @param response HttpServletResponse object
     * @throws IOException close HttpServletResponse error
     */
    public static void write(HttpServletResponse response, int status, String message) throws IOException {
        response.setStatus(status);
        write(response, JsonWrapper.builder().setStatus(status).setMsg(message).build());
    }

    /**
     * 返回数据对象 o
     *
     * @param resp HttpServletResponse 对象
     * @param o    写回的对象
     * @throws IOException 关闭流导致的错误
     */
    public static void write(HttpServletResponse resp, Object o) throws IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        try (OutputStream os = resp.getOutputStream()) {
            os.write(o.toString().getBytes("UTF-8"));
            os.flush();
        }
    }
}
